package com.xjrh.traintravel.collector.controller;

import com.alibaba.fastjson.JSONObject;
import com.xjrh.common.core.utils.ObjectUtils;
import com.xjrh.common.core.web.AjaxResult;
import com.xjrh.common.core.web.BaseController;
import com.xjrh.traintravel.collector.common.CRC16M;
import com.xjrh.traintravel.collector.common.CodeConvertor;
import com.xjrh.traintravel.collector.controller.param.FaultCheckResult;
import com.xjrh.traintravel.collector.controller.param.FaultDetails;
import com.xjrh.traintravel.collector.netty.coder.command.AgreementCommandEnum;
import com.xjrh.traintravel.collector.netty.coder.command.AgreementStructure;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;

/**
 * 测试Controller
 *
 * @author snps
 * @create 2024-11-15 14:20
 * @describe
 **/
@RequestMapping("/test")
@RestController
@Slf4j
public class TestController extends BaseController {

    /**
     * 构造故障检测结果上报测试协议
     */
    @PostMapping("/buildFaultTestAgreement")
    public AjaxResult buildFaultTestAgreement(@RequestBody FaultCheckResult param) {
        if (ObjectUtils.isNull(param)) {
            return error("请求参数不能为空！");
        }
        // log.warn("收到请求参数: {}", JSONObject.toJSONString(param));

        // 构造 协议内容（DATA区域）
        // 标识
        StringBuilder sbuData = new StringBuilder("04");
        // 故障类型
        sbuData.append(CodeConvertor.intToHexString(param.getFaultType(), 1));
        // 时间（年月日时分秒）
        Calendar calendar = Calendar.getInstance();
        sbuData.append(CodeConvertor.intToHexString(calendar.get(Calendar.YEAR)-2000, 1));
        sbuData.append(CodeConvertor.intToHexString(calendar.get(Calendar.MONTH)+1, 1));
        sbuData.append(CodeConvertor.intToHexString(calendar.get(Calendar.DATE), 1));
        sbuData.append(CodeConvertor.intToHexString(calendar.get(Calendar.HOUR_OF_DAY), 1));
        sbuData.append(CodeConvertor.intToHexString(calendar.get(Calendar.MINUTE), 1));
        sbuData.append(CodeConvertor.intToHexString(calendar.get(Calendar.SECOND), 1));
        // 段、列、车
        sbuData.append(param.getSegment());
        sbuData.append(param.getTrain());
        sbuData.append(param.getCar());
        // COM口、前置、传感器
        sbuData.append(CodeConvertor.intToHexString(param.getComPort(), 1));
        sbuData.append(CodeConvertor.intToHexString(param.getFront(), 1));
        sbuData.append(CodeConvertor.intToHexString(param.getSensor(), 1));
        // 故障数量
        sbuData.append(CodeConvertor.intToHexString(param.getFaultCount(), 1));
        // 故障详细信息
        StringBuilder sbuFaultDetails = new StringBuilder("");
        if (ObjectUtils.isNotEmpty(param.getFaultList())) {
            for (FaultDetails faultDetails : param.getFaultList()) {
                sbuFaultDetails.append(faultDetails.getType1());
                // sbuFaultDetails.append(faultDetails.getType2());
                sbuFaultDetails.append(CodeConvertor.intToHexString(faultDetails.getType2(), 1));
                sbuFaultDetails.append(CodeConvertor.intToHexString(faultDetails.getFaultLevel(), 1));
                sbuFaultDetails.append(CodeConvertor.intToHexString(faultDetails.getDbValue(), 4));
                sbuFaultDetails.append(CodeConvertor.intToHexString(faultDetails.getPolygon(), 1));
            }
        }
        sbuData.append(sbuFaultDetails);

        // 构造 协议对象
        AgreementStructure agreement = new AgreementStructure(AgreementCommandEnum.S_0D.getValue(), param.getHostNumber(), "01");
        agreement.setLen(CodeConvertor.intToHexString(sbuData.length()/2, 2));
        agreement.setData(sbuData.toString());
        String crcValue = CRC16M.calculateCrc(agreement);
        agreement.setFcs(crcValue);
        // log.warn("构造的协议内容: {}", agreement);

        return success(agreement.toString());
    }


}